﻿//////////////////////////////////////////////////////////////////
//
// WorldInfoData.cs
//
// Copyright (c) 2012 Dan Pike. All rights reserved
//
// Refer to license.txt for any license restrictions.
// 
using Entities;
using Logger4Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;

namespace Sally
{
   public class WorldInfoData
   {
      //////////////////////////////////////////////////////////////////////////
      //
      public WorldInfoData(World world)
      {
         world_ = world;
      }

      //////////////////////////////////////////////////////////////////////////
      //
      private WorldInfoCity getInfoCity(City city)
      {
         WorldInfoCity infoCity = null;
         try
         {
            if (!cities_.TryGetValue(city, out infoCity))
            {
               cities_[city] = infoCity = new WorldInfoCity(city);
            }
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
         return infoCity;
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public void setPopulation(City city, int population)
      {
         try
         {
            getInfoCity(city).population = population;
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public void setFactory(City city, Factory factory, int count)
      {
         try
         {
            getInfoCity(city).setFactory(factory, count);
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public void setConvoy(string name, int capacity, List<City> ports)
      {
         try
         {
            if (!convoyPorts_.ContainsKey(name))
            {
               convoyPorts_.Add(name, ports);
               convoyCapacities_.Add(name, capacity);
            }
            else
            {
               if (0 < ports.Count)
               {
                  // This is the entry that lists the ports
                  convoyPorts_[name] = ports;
               }
               if (0 < capacity)
               {
                  // This is the entry that lists the convoy details
                  convoyCapacities_[name] = capacity;
               }
            }
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public Dictionary<City, WorldInfoCity> infoCities
      {
         get { return cities_; }
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public Dictionary<string, List<City>> infoConvoyPorts
      {
         get { return convoyPorts_; }
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public Dictionary<string, int> infoConvoyCapacities
      {
         get { return convoyCapacities_; }
      }

      //////////////////////////////////////////////////////////////////////////
      //
      private static Logger logger_ = new Logger(typeof(Main));
      private World world_;
      private Dictionary<City, WorldInfoCity> cities_ = new Dictionary<City, WorldInfoCity>();
      private Dictionary<string, List<City>> convoyPorts_ = new Dictionary<string, List<City>>();
      private Dictionary<string, int> convoyCapacities_ = new Dictionary<string, int>();
   }
}
